var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        ID:0,
        PointID:0,
        ItemsID:0,
        CheckPoint:'',
        CheckPointIndex:'',
        CheckPointType:'',
        Contents:'',
        form: {
            ID: 0,
            StatusDesc: '',
            action: 'customerserviceprocess',
            issuggestion: 0,
            processType: 1,//1-处理2-完工3-回访
            RelationIDList: ''
        },
        writeCount:0,
        imglist: [],
        imgindex: 0,
        img_count: 0,
        select_imglist: [],
        select_imgindex: 0,
        maxuploadcount: CONFIG.maxuploadcount,
        type:1,//1-处理2-完工3-回访
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                ItemsID: that.ItemsID,
                action: 'getcheckprojectlist',
            }, function(succeed, data, err) {
                if (succeed) {
                    if(data.status){
                        that.CheckPoint = data.data.CheckPoint;
                        that.CheckPointIndex = data.data.CheckPointIndex;
                        that.CheckPointType = data.data.CheckPointType;
                    }else{
                        api.toast({
                            msg:'数据错了',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_savezhengai:function(){
            var that = this;
            that.do_save(1);

                
           
        },
        writeMsg:function(){
            var that=this;
            var testVal=that.Contents.length;
            that.writeCount=testVal;
        },
        do_save: function(type) {
            var that = this;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }
            var options = {};
            options.ID = that.ID;
            options.PlanID = that.PointID;
            options.Contents=that.Contents;
            options.action = "savacheckproject";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    ns.toast('保存成功');
                    api.sendEvent({
                        name: 'loadservicelist',
                    });
                    if(type==1){
                        ns.openWin('qualitycontrol_frm', '新增整改工单',{
                            ComeFrom:3,
                            RelatedID:that.PointID,
                            PlanID:that.ID
                        })
                    }
                    
                    setTimeout(function() {
                        api.closeWin();
                    }, 500);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: filelist
                },
                toast: true,
                toastmsg: '提交中'
            });
        },
        choose_picture: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();
            })
        },
        choose_picture_start: function() {
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            UIAlbumBrowser.imagePicker({
                max: that.maxuploadcount - that.imglist.length,
                styles: {
                    bg: '#000000',
                    mark: {
                        position: 'top_right',
                        size: 20
                    },
                    nav: {
                        bg: '#000000',
                        cancelColor: '#fff',
                        cancelSize: 16,
                        nextStepColor: '#7fff00',
                        nextStepSize: 16
                    },
                    thumbnail: { //（可选项）返回的缩略图配置，**建议本图片不要设置过大** 若已有缩略图，则使用已有的缩略图。若要重新生成缩略图，可先调用清除缓存接口api.clearCache()。
                        w: 100, //（可选项）数字类型；返回的缩略图的宽；默认：原图的宽度
                        h: 100 //（可选项）数字类型；返回的缩略图的宽；默认：原图的高度
                    }
                },
                animation: true,
            }, function(ret) {
                if (ret.eventType == 'nextStep') {
                    if (ret.list && ret.list.length > 0) {
                        that.select_imglist = ret.list;
                        that.select_imgindex = 0;
                        that.UIAlbumBrowser_transPath();
                    }
                    UIAlbumBrowser.closePicker();
                }
                if (ret.originalPath && ret.originalPath.length > 0) {
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.originalPath,
                        thumbPath: ret.originalPath
                    });
                    that.imgindex++;
                }
            });
        },
        UIAlbumBrowser_transPath: function() {
            var that = this;
            var item = that.select_imglist[that.select_imgindex];
            UIAlbumBrowser.transPath({
                path: item.path
            }, function(ret, err) {
                if (ret) {
                    that.select_imgindex++;
                    if (that.select_imgindex < that.select_imglist.length) {
                        that.UIAlbumBrowser_transPath();
                    } else {
                        that.select_imgindex = 0;
                    }
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.path,
                        thumbPath: item.thumbPath
                    });
                    that.imgindex++;
                }
            });
        },
        remove_img: function(index) {
            var that = this;
                    for (var i = 0; i < that.imglist.length; i++) {
                        var item = that.imglist[i];
                        if (item.index == index) {
                            that.imglist.splice(i, 1);
                        }
                    }
        },
        
       
       
        // do_remove_product: function(item) {
        //     var that = this;
        //     api.confirm({
        //         title: '提示',
        //         msg: '确定删除?',
        //         buttons: ['确定', '取消']
        //     }, function(ret, err) {
        //         if (ret.buttonIndex == 1) {
        //             for (var i = 0; i < that.productlist.length; i++) {
        //                 if (that.productlist[i].ID == item.ID) {
        //                     that.productlist.splice(i, 1);
        //                 }
        //             }
        //         }
        //     });
        // },

    }
});

apiready = function() {
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    app.ItemsID = api.pageParam.CheckpointsID || 0;
    app.ID = api.pageParam.ID || 0;
    app.PointID = api.pageParam.PointID || 0;
    // app.form.processType=api.pageParam.type;
    // app.form.issuggestion = api.pageParam.issuggestion || 0;
    app.get_data();
    api.addEventListener({
        name: 'do_choose_ckproduct_complete'
    }, function(ret, err) {
        if (ret) {
            app.get_product_list(ret.value.list);
        }
    });
    api.addEventListener({
        name: 'dosaveservicefeedone'
    }, function(ret, err) {
        app.form.RelationIDList = ret.value.IDList;
    });
    api.addEventListener({
        name: 'reloadservicelist'
    }, function(ret, err) {
        api.closeWin();
    });

}
